
package net.vengeancecraft.NoPortals;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
 *
 * @authors Gamolin Mistwalker and Kerovan Mistwalker
 */


public class FileHandler {
    static String mainDirectory = "plugins/NoPortals";
    String eol = System.getProperty("line.separator");  

    public void loadPortalsFromFile(String filename) throws IOException
    {
        try
        {
            File myFile = new File(mainDirectory + File.separator + filename + ".log");
            if(myFile.exists())
            {
                BufferedReader readFile = new BufferedReader(new FileReader(myFile));

                String readPortal;

                while((readPortal = readFile.readLine()) != null)
                {
                    if("ValidPortals".equals(filename))
                    {
                        NoPortals.validPortals.put(readPortal.toString(), Boolean.TRUE);
                    }
                    
                }
                readFile.close();
            }
        
        
        }
        catch (FileNotFoundException ex)
        {
            Logger.getLogger(FileHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    
    }
      
    public void writeToFile(String filename, String blockLocation){
       
        File NoPortals = new File(mainDirectory + File.separator + filename + ".log");
        new File(mainDirectory).mkdir(); 
        if (!NoPortals.exists())
        {
            try
            {
                NoPortals.createNewFile();
                FileWriter fstream = new FileWriter(NoPortals);
                BufferedWriter out = new BufferedWriter(fstream);
                out.write(blockLocation + eol);
                out.close();
            }
            catch (IOException ex)
            {
               Logger.getLogger(FileHandler.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        else
        {
            try
            {
                FileWriter fstream = new FileWriter(NoPortals, true);
                BufferedWriter out = new BufferedWriter(fstream);
                out.write(blockLocation + eol);
                out.close();
            }
            catch (IOException ex)
            {
                Logger.getLogger(FileHandler.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
       
    }
    
    
        
        
        
    }    
    

